package com.smart.stock;

import com.smart.stock.common.startup.InitQuartzTaskRunner;
import com.smart.stock.common.util.cache.EhcacheUtil;
import com.smart.stock.module.spider.jobService.ITimeJobFactory;
import com.smart.stock.module.spider.jobService.ITimerJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.net.InetAddress;
import java.util.Date;

/**
 * spring boot 启动类
 *
 * @author kings.chan
 */
@Controller
@EnableCaching
@ServletComponentScan
@SpringBootApplication
public class Application {

    @Autowired
    private EhcacheUtil ehcacheUtil;

    @RequestMapping("/")
    public String index(Model model) {
        model.addAttribute("industry", ehcacheUtil.getKey("App_init","ths_type"));//同花顺类型
        model.addAttribute("zzfl", ehcacheUtil.getKey("App_init","zzfl"));//中证分类
        return "index";
    }

    @Bean
    public InitQuartzTaskRunner startupRunner() {
        return new InitQuartzTaskRunner();
    }


    public static void main(String[] args) throws Exception {
//        SpringApplication app = new SpringApplication(Application.class);
//        app.setBannerMode(Banner.Mode.OFF);
//        app.run(args);

        ConfigurableApplicationContext app = SpringApplication.run(Application.class, args);
        ConfigurableEnvironment env = app.getEnvironment();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path");
        if (path == null) path = "";
        String ip = InetAddress.getLocalHost().getHostAddress();

        System.out.println("Application started ..........");
        System.out.println("http://localhost:"+port+path);
        System.out.println("http://"+ip+":"+port+path);
        System.out.println(new Date());

        try {
            ITimeJobFactory.getJob(ITimeJobFactory.TIMEJOB.CORE_SCHEDULE).execute(ITimerJob.COMMAND.START);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}